﻿Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Imports System.Collections.Generic

Namespace Pic
    Public Class Comments
        Inherits clsBase
#Region "Private Variables"
        Private _ObjectId As Integer
        Private _UserEmail As String
        Private _Comment As String
        Private _Created As DateTime
        Private _Modified As DateTime
#End Region
#Region "Properties"
        Public Property ObjectId As Integer
            Set(value As Integer)
                _ObjectId = value
            End Set
            Get
                Return _ObjectId
            End Get
        End Property
        Public Property UserEmail As String
            Set(value As String)
                _UserEmail = value
            End Set
            Get
                Return _UserEmail
            End Get
        End Property
        Public Property Comment As String
            Set(value As String)
                _Comment = value
            End Set
            Get
                Return _Comment
            End Get
        End Property
        Public Property Created As DateTime
            Set(value As DateTime)
                _Created = value
            End Set
            Get
                Return _Created
            End Get
        End Property
        Public Property Modified As DateTime
            Set(value As DateTime)
                _Modified = value
            End Set
            Get
                Return _Modified
            End Get
        End Property
#End Region
#Region "Constructor"
        Public Sub New(ByVal ObjectId_ As Integer, ByVal UserEmail_ As String, ByVal Comment_ As String, ByVal Created_ As DateTime, ByVal Modified_ As DateTime)
            Me.ObjectId = ObjectId_
            Me.UserEmail = UserEmail_
            Me.Comment = Comment_
            Me.Created = Created_
            Me.Modified = Modified_
        End Sub
#End Region
#Region "Methods"
        Private Shared Function GetCommentsFromReader(ByVal reader_ As IDataReader) As Comments
            Dim rtrn As Comments = New Comments(reader_("ObjectId"), reader_("UserEmail"), reader_("Comment"), reader_("Created"), reader_("Modified"))
            Return rtrn
        End Function
        Public Shared Function GetCommentsById(ByVal ObjectId_ As Integer, UserEmail_ As String, Created_ As DateTime) As Comments
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select * from tbl_Comments where object_id=@ObjectId and user_email=@UserEmail and created=@Created"
            Dim reader As IDataReader
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text

                cmd.Parameters.Add("@ObjectId", SqlDbType.Int).Value = ObjectId_
                cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                cmd.Parameters.Add("@Created", SqlDbType.DateTime).Value = Created_
                reader = ExecuteReader(cmd, CommandBehavior.SingleRow)
                If reader.Read Then
                    Return GetCommentsFromReader(reader)
                Else
                    Return Nothing
                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Private Shared Function GetCommentsListFromReader(ByVal reader As IDataReader) As List(Of Comments)
            Dim rtrnList As New List(Of Comments)
            Do While reader.Read
                rtrnList.Add(GetCommentsFromReader(reader))
            Loop
            Return rtrnList
        End Function
        Public Shared Function GetCommentsListByFilter(ByVal Filtre_ As String, ByVal FiltreOrder_ As String) As List(Of Comments)
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "select * from website1.dbo.tbl_Comments where " & Filtre_ & "' order by " & FiltreOrder_ & " desc"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                Return GetCommentsListFromReader(ExecuteReader(cmd, CommandBehavior.Default))
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function InsertComment(ByVal ObjectID_ As Integer, ByVal UserEmail_ As String, ByVal Comment_ As String) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "insert into tbl_Comments(object_id,user_email,comment,created,modified) values(@ObjectID,@UserEmail,@Comment,@created,@modified)"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@ObjectID", SqlDbType.Int).Value = ObjectID_
                cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                cmd.Parameters.Add("@Comment", SqlDbType.Text).Value = Comment_
                cmd.Parameters.Add("@created", SqlDbType.DateTime).Value = Now.Date
                cmd.Parameters.Add("@modified", SqlDbType.DateTime).Value = Now.Date
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function

        Public Shared Function UpdateComment(ByVal ObjectID_ As Integer, ByVal UserEmail_ As String, ByVal Comment_ As String, ByVal Created_ As DateTime) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "update tbl_Comments set object_id=@ObjectID,user_email=@UserEmail,comment=@Comment,Modified=@modified where object_id=@ObjectID and user_email=@UserEmail and created=@Created"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@ObjectID", SqlDbType.Int).Value = ObjectID_
                cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                cmd.Parameters.Add("@Comment", SqlDbType.Text).Value = Comment_
                cmd.Parameters.Add("@created", SqlDbType.DateTime).Value = Created_
                cmd.Parameters.Add("@modified", SqlDbType.DateTime).Value = Now.Date
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function DeleteComment(ByVal ObjectID_ As Integer, ByVal UserEmail_ As String, ByVal Created_ As DateTime) As Boolean
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select count(1) from tbl_Comments where object_id=@ObjectID and user_email=@UserEmail and created=@Created"
            Dim count As Integer
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.Parameters.Add("@ObjectID", SqlDbType.Int).Value = ObjectID_
                cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                cmd.Parameters.Add("@created", SqlDbType.DateTime).Value = Created_
                count = ExecuteScalar(cmd)
                If count = 0 Then
                    Return False
                Else
                    cmd.CommandText = "Delete from tbl_Comments where object_id=@ObjectID and user_email=@UserEmail and created=@Created"
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.Add("@ObjectID", SqlDbType.Int).Value = ObjectID_
                    cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                    cmd.Parameters.Add("@created", SqlDbType.DateTime).Value = Created_
                    Return ExecuteNonQuery(cmd)

                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function

        'Public Shared Function SilinebilirMi(ByVal ProjeID_ As Integer) As Boolean
        '    Dim conn As New SqlConnection(ConnectionString)
        '    Dim veriString As String = "Select count(1) from tblSondajlar where ProjeID=" & ProjeID_
        '    Dim count As Integer
        '    Try
        '        conn.Open()
        '        Dim cmd As New SqlCommand(veriString, conn)
        '        count = ExecuteScalar(cmd)
        '        If count = 0 Then
        '            Return True
        '        Else
        '            Return False
        '        End If
        '    Catch ex As Exception
        '        Throw ex
        '    Finally
        '        conn.Close()
        '    End Try
        'End Function
#End Region

    End Class
End Namespace


